package com.jiaoyou.baojian.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.adapter.NewsAdapter;
import com.jiaoyou.baojian.entity.NewsEntity;
import com.jiaoyou.baojian.entity.response.NewsResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;

public class NewsActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private int page = 1;
    private int pageSize = 10;

    private List<NewsEntity> newsList;
    private NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        setTitle("消息");

        newsList = new ArrayList<>();
        adapter = new NewsAdapter();
        adapter.setEnableLoadMore(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setAdapter(adapter);
        recyclerview.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(getResources().getColor(R.color.background))
                .size(1).build());
        adapter.setEmptyView(getEmptyView());
        adapter.setNewData(newsList);
        adapter.notifyDataSetChanged();
        adapter.setOnLoadMoreListener(this, recyclerview);

        getData();
    }

    private View getEmptyView() {
        TextView textView = new TextView(this);
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("数据为空");
        return textView;
    }

    private void getData() {
        Api.getInstance().getMessageList(page, pageSize, new BaofangCallback<NewsResponse>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                toast("获取数据错误，请稍后重试");
                finish();
            }

            @Override
            public void onSuccess(NewsResponse response) {
                adapter.loadMoreComplete();
                if(page == 1) {
                    newsList = response.data;
                    adapter.setNewData(newsList);
                } else {
                    newsList.addAll(response.data);
                }
                adapter.notifyDataSetChanged();
                if((response.pageNo * response.pageSize) > response.total){
                    adapter.loadMoreEnd();
                }
            }
        });
    }

    @Override
    public int getContentView() {
        return R.layout.activity_news;
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getData();
    }
}
